package club.hicode.daydayup.netty.demo.server;

import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;
import lombok.extern.slf4j.Slf4j;

/**
 * 自己的 ChannelInBoundHandler
 *
 * @author Liu Chunfu
 * @date 2018-06-14 下午2:58
 **/
@Slf4j
public class MyChannelInitializer extends ChannelInitializer {


    /**
     * 分隔符
     */
    private static final String DELIMITER = "$";

    static final EventExecutorGroup group = new DefaultEventExecutorGroup(16);

    @Override
    protected void initChannel(Channel channel) throws Exception {
        ChannelPipeline pipeline = channel.pipeline();

        pipeline.addLast(" delimiter", new DelimiterBasedFrameDecoder(1024 * 8, Unpooled.copiedBuffer(DELIMITER.getBytes())));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());//如果需要发送结果必须在这个加入解码器
        //pipeline.addLast("handler", new MyHandler());
        pipeline.addLast(group, "handler", new MyHandler());
        pipeline.addLast("last", new LastHandler());

        log.info("初始化成功。。。");
        //channel.pipeline().addLast("myHandler", new MyHandler());
    }

}
